package dao.impl;

import dao.AirInfoDao;
import pojo.AirInfo;
import util.BaseUtil;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class AirInfoDaoImpl extends BaseUtil implements AirInfoDao {
    @Override
    public void getAll() {
        AirInfo airInfo = new AirInfo();
        try {
            String sql = "select * from airinfo";
            ResultSet resultSet = null;
            resultSet = stSelect(sql);
            System.out.println("编号\t\t航班号\t\t目的地\t\t\t起飞日期\t\t\t留言");
            while (resultSet.next()) {
                String kg = "\t\t";
                //1.读取存储信息
                airInfo.setAirid(resultSet.getInt("airid"));
                airInfo.setAirname(resultSet.getString("airname"));
                airInfo.setAircity(resultSet.getString("aircity"));
                if (airInfo.getAircity().length()<8){kg = "\t\t\t";}
                airInfo.setAirdate(resultSet.getString("airdate"));
                airInfo.setAirword(resultSet.getString("airword"));
                System.out.println(airInfo.getAirid()+"\t\t\t"+airInfo.getAirname()+"\t\t\t"+
                    airInfo.getAircity()+kg+airInfo.getAirdate()+"\t\t\t"+airInfo.getAirword());
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            try {
                closeAll();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public void getByDate(String airDate) {
        AirInfo airInfo = new AirInfo();
        try {
            String sql = "select * from airinfo where airdate = ?";
            List list = new ArrayList();
            list.add(airDate);
            ResultSet resultSet = null;
            resultSet = stSelect(sql, list);
            System.out.println("编号\t\t航班号\t\t目的地\t\t\t起飞日期\t\t\t留言");
            while (resultSet.next()) {
                String kg = "\t\t";
                //1.读取存储信息
                airInfo.setAirid(resultSet.getInt("airid"));
                airInfo.setAirname(resultSet.getString("airname"));
                airInfo.setAircity(resultSet.getString("aircity"));
                if (airInfo.getAircity().length()<8){kg = "\t\t\t";}
                airInfo.setAirdate(resultSet.getString("airdate"));
                airInfo.setAirword(resultSet.getString("airword"));
                System.out.println(airInfo.getAirid()+"\t\t\t"+airInfo.getAirname()+"\t\t\t"+
                        airInfo.getAircity()+kg+airInfo.getAirdate()+"\t\t\t"+airInfo.getAirword());
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            try {
                closeAll();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public void getByCity(String airCity) {
        AirInfo airInfo = new AirInfo();
        try {
            String sql = "select * from airinfo where aircity = ?";
            List list = new ArrayList();
            list.add(airCity);
            ResultSet resultSet = null;
            resultSet = stSelect(sql, list);
            System.out.println("编号\t\t航班号\t\t目的地\t\t\t起飞日期\t\t\t留言");
            while (resultSet.next()) {
                String kg = "\t\t";
                //1.读取存储信息
                airInfo.setAirid(resultSet.getInt("airid"));
                airInfo.setAirname(resultSet.getString("airname"));
                airInfo.setAircity(resultSet.getString("aircity"));
                if (airInfo.getAircity().length()<8){kg = "\t\t\t";}
                airInfo.setAirdate(resultSet.getString("airdate"));
                airInfo.setAirword(resultSet.getString("airword"));
                System.out.println(airInfo.getAirid()+"\t\t\t"+airInfo.getAirname()+"\t\t\t"+
                        airInfo.getAircity()+kg+airInfo.getAirdate()+"\t\t\t"+airInfo.getAirword());
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            try {
                closeAll();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public void deleteById(int airId) {
        try {
            String sql = "DELETE FROM airinfo WHERE airid = ?";
            List list = new ArrayList();
            list.add(airId);
            int reture = stUpdate(sql, list);
            if (reture>0){
                System.out.println("删除成功！");
            }else {
                System.out.println("删除失败！");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            try {
                closeAll();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public void updateById(List list) {
        try {
            String sql = "UPDATE airinfo SET airname = ?,aircity = ?,airdate = ? WHERE airid = ?";
            int reture = stUpdate(sql, list);
            if (reture>0){
                System.out.println("更新成功！");
            }else {
                System.out.println("更新失败！");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            try {
                closeAll();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public void setById(List list) {
        try {
            String sql = "UPDATE airinfo SET airword = ? WHERE airid = ?";
            int reture = stUpdate(sql, list);
            if (reture>0){
                System.out.println("留言成功！");
            }else {
                System.out.println("留言失败！");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            try {
                closeAll();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
}
